using UnityEngine;
using System.Collections;
using System.Collections.Generic;


// A Game is a set of turn played by players, whith some goals to acheive


// Used to sort effect by priority, and determine Which one is gonna go first
class EffectComparer : IComparer<Effect>
{
    public int Compare(Effect x,Effect y)
    {
      if (x.Priority > y.Priority )
         return 1;
      if (x.Priority < y.Priority)
         return -1;
      else
         return 0;
    }
}

public class Game : MonoBehaviour {
	
	// Properties
	//
	
	public Player Player1;
	public Player Player2;
	
	public List<Player> Players;
	
	public List<Turn> TurnPlayed;
	
	public Turn CurrentTurn;
	
	public int TurnNumber = 1;
	
	// Method
	//
	
	// Use this for initialization
	void Start () {
	
	}
	
	// Update is called once per frame
	void Update () {
	
	}
	
	// Create a new turn and start it
	public void StartNewTurn(){
						
		// Create a new turn
		Turn newTurn = new Turn(this.TurnNumber);
		
		// Set the current Phase 
		newTurn.CurrentTurnPhase = Turn.TurnPhase.GenerateIncome;
		
		this.CurrentTurn = newTurn;
		
		
		
	}
	
	
	// End the current turn
	public void EndTurn(Turn Turn){
		
		if(Turn.CurrentTurnPhase == Turn.TurnPhase.done){
			
			this.TurnPlayed.Add(Turn);
			
			
			// Checking for victory			
			bool p1GoalMet = this.Player1.CheckGoalFullfilment();
			
			bool p2GoalMet = this.Player2.CheckGoalFullfilment();
			
			if(p1GoalMet && !p2GoalMet){
				
				// TODO P1 victory
				
			} else if(p2GoalMet && ! p1GoalMet){
				
				// TODO P2 Victory
				
			} else if(p2GoalMet && p1GoalMet){
				
				// TODO Draw
				
			} else {
				
				// New turn is playing
				this.TurnNumber ++;
				this.StartNewTurn();
				
			}
			
			
			
		}
		else {
			
			// Turn is not done !
			
		}
		
	}
	
	
	public void GenerateIncome(){
		
		// Player 1 ressources income
		this.Player1.Magic += this.Player1.Gems;
		this.Player1.Rocks += this.Player1.Mines;
		this.Player1.Beast += this.Player1.Dungeons;
		
		// Player 2 ressources income
		this.Player2.Magic += this.Player2.Gems;
		this.Player2.Rocks += this.Player2.Mines;
		this.Player2.Beast += this.Player2.Dungeons;
		
		
		// TODO : If we play an animation or set a waiting screen, it should be done else where
		
	}
	
	// Play the cards of each player for this turn
	public void PlayCards(int CardNumber){
		
		List<Effect> effectList = new List<Effect>();
		
		if(this.CurrentTurn.CardPlayer1[CardNumber] != null){
			
			Card c = this.CurrentTurn.CardPlayer1[CardNumber];
			
			// Check if the player can pay the cost of this card
			if(c.BeastCost <= this.Player1.Beast && c.MagicCost <= this.Player1.Magic && c.RocksCost <= this.Player1.Rocks){
				
				// Remove ressources from player
				this.Player1.Magic -= c.MagicCost;
				this.Player1.Rocks -= c.RocksCost;
				this.Player1.Beast -= c.BeastCost;
							
				// For each effect in this card
				foreach (Effect e in c.Effects){
					effectList.Add(e);
				}
				
			}
			
		}
		
		if(this.CurrentTurn.CardPlayer2[CardNumber] != null){
			
			Card c = this.CurrentTurn.CardPlayer2[CardNumber];
			
			// Check if the player can pay the cost of this card
			if(c.BeastCost <= this.Player2.Beast && c.MagicCost <= this.Player2.Magic && c.RocksCost <= this.Player2.Rocks){
				
				// Remove ressources from player
				this.Player2.Magic -= c.MagicCost;
				this.Player2.Rocks -= c.RocksCost;
				this.Player2.Beast -= c.BeastCost;
							
				// For each effect in this card
				foreach (Effect e in c.Effects){
					effectList.Add(e);
				}
				
			}
			
		}
		
		// Sort the list of effect
		effectList.Sort(new EffectComparer());
		
		// Play the effects in the sorted order		
		foreach(Effect e in effectList){
						
			e.ApplyEffect();
			
		}
		
	}
	
	public void ResolutionPhase(){
		
		
		if(this.CurrentTurn.CurrentTurnPhase == Turn.TurnPhase.ResolutionCard1){
			
			// Play the first two cards
			this.PlayCards(0);
			this.CurrentTurn.CurrentTurnPhase = Turn.TurnPhase.ResolutionCard2;
			
		}
		else if(this.CurrentTurn.CurrentTurnPhase == Turn.TurnPhase.ResolutionCard2){
			
			// Play the 2nd two cards
			this.PlayCards(1);
			this.CurrentTurn.CurrentTurnPhase = Turn.TurnPhase.ResolutionCard3;
			
		} else if(this.CurrentTurn.CurrentTurnPhase == Turn.TurnPhase.ResolutionCard3){
			
			// Play the 3rd two cards
			this.PlayCards(2);
			// Go to the next phase of the turn
			this.CurrentTurn.CurrentTurnPhase = Turn.TurnPhase.ClearHandPlayer1;
			
		}
		
	}
	
	public void Player1DiscardHand(){
		
		if(this.CurrentTurn.CurrentTurnPhase == Turn.TurnPhase.ClearHandPlayer1){
			
			if(this.Player1.CurrentHand.Cards.Count > Hand.HandMaxSize){
				
				// TODO : Show Interface to discard cards
				
				// Reinvoke this function right after
				this.Player1DiscardHand();
				
			}
			else{
				
				this.CurrentTurn.CurrentTurnPhase = Turn.TurnPhase.ClearHandPlayer2;
				
			}
			
		}
		
	}
	
	
	public void Player2DiscardHand(){
		
		if(this.CurrentTurn.CurrentTurnPhase == Turn.TurnPhase.ClearHandPlayer2){
			
			if(this.Player2.CurrentHand.Cards.Count > Hand.HandMaxSize){
				
				// TODO : Show Interface to discard cards
				
				// Reinvoke this function right after
				this.Player2DiscardHand();
				
			}
			else{
				
				this.CurrentTurn.CurrentTurnPhase = Turn.TurnPhase.done;
				
			}
			
		}
		
	}
	
	public void PickCard(Card card, int CardNumber, int playerNumber){
		
		card.inHand = null;
		card.inTurn = this.CurrentTurn;
		
		if(playerNumber == 1){
			
			// First remove the current card
			if(this.CurrentTurn.CardPlayer1[CardNumber] != null){
				
				this.RepickCard(CardNumber, playerNumber);
				this.PickCard(card, CardNumber, playerNumber);
				
			}
			// Pick this card
			else {
				
				card.Owner = this.Player1;
				this.CurrentTurn.CardPlayer1[CardNumber] = card;
				
			}
			
		}
		else if(playerNumber == 2){
			
			// First remove the current card
			if(this.CurrentTurn.CardPlayer2[CardNumber] != null){
				
				this.RepickCard(CardNumber, playerNumber);
				this.PickCard(card, CardNumber, playerNumber);
				
			}
			// Pick this card
			else {
				
				card.Owner = this.Player2;
				this.CurrentTurn.CardPlayer2[CardNumber] = card;
				
			}
			
		}		
		
		
	}
	
	// Send back the card in this number in player hand
	public void RepickCard(int CardNumber, int playerNumber){
		
		if(playerNumber == 1 && this.CurrentTurn.CardPlayer1[CardNumber] != null){
			
			Card repickedCard = this.CurrentTurn.CardPlayer1[CardNumber];
			
			repickedCard.inTurn = null;
			repickedCard.inHand = this.Player1.CurrentHand;
			
			this.CurrentTurn.CardPlayer1[CardNumber] = null;
			this.Player1.CurrentHand.Cards.Add(repickedCard);
			// TODO show the card repicked in hand
			
		}
		else if(playerNumber ==2 && this.CurrentTurn.CardPlayer2[CardNumber] != null){
			
			Card repickedCard = this.CurrentTurn.CardPlayer2[CardNumber];
			
			repickedCard.inTurn = null;
			repickedCard.inHand = this.Player2.CurrentHand;
			
			this.CurrentTurn.CardPlayer1[CardNumber] = null;
			this.Player2.CurrentHand.Cards.Add(repickedCard);
			// TODO show the card repicked in hand
			
		}
		
	}
	
}
